package com.panda.pandawash.model;

import android.util.Log;

/**
 * Created by gaowf on 2017/2/22.
 */

public class MotorDeviceInfoMessage extends MotoBaseMessage {

    public final static String MOTOR_XIANGWEI = "motor_xiangwei";
    public final static String MOTOR_VERSION = "motor_version";
    public final static String MOTOR_SUBNUM = "motor_subnum";
    public final static String MOTOR_CHECK_REAL = "motor_isreal";
    public final static String MOTOR_WORKING_CURRENT = "motor_working_current";
    public final static String MOTOR_FIRST_AJUST_CURRENT = "motor_fa_current";
    public final static String MOTOR_SECOND_AJUST_CURRENT = "motor_sa_current";
    public final static String MOTOR_TAN_VALUE = "motor_tan_value";
    public final static String MOTOR_LREF_VOLT = "motor_lref_volt";
    public final static String MOTOR_ENABLE_DETECT_LEAK = "motor_enable_leak";
    public final static String MOTOR_SHUTGUN_DELAY = "motor_shutgun_delay";
    public final static String MOTOR_SHUTDOWN_DELAY = "motor_shutdown_delay";
    public final static String MOTOR_START_SELECTED_WAYS = "motor_startup_ways";
    public final static String MOTOR_PRODUCT_DATE = "motor_product_date";
    public final static String MOTOR_RUNED_TIME = "motor_runed_time";
    public final static String MOTOR_WORKING_FREQ = "motor_working_freq";
    public final static String MOTOR_START_TIMES = "motor_start_times";
    public final static String MOTOR_OVERLOAD_SHIELD = "motor_overload_shield";
    public final static String MOTOR_STARTGUN_DELAY = "motor_startgun_delay";
    public final static String MOTOR_PRODUCTOR_NAME = "washer_productor";
    public final static String MOTOR_USED_ENERGY = "motor_used_energy";
    public final static String MOTOR_USED_WATER_TIME = "motor_used_water_time";
    public final static String MOTOR_SERIAL_ID = "motor_serial_id";
    public final static String MOTOR_MAX_RUNNING_TIME = "motor_max_running";
    public final static String MOTOR_FULLLOAD_PARA = "motor_full_load";
    public final static String MOTOR_EMPTYLOAD_PARA = "motor_empty_load";

    public final static String MOTOR_SHUTGUN_DELAY_VALUE = "motor_shutgun_delay_value";
    public final static String MOTOR_STARTGUN_DELAY_VALUE = "motor_startgun_delay_value";
    public final static String MOTOR_MAX_CONTINUE_RUN_VALUE = "motor_max_continue_run_value";
    public final static String MOTOR_SJ_DELAY_VALUE = "motor_sj_delay_value";
    public final static String MOTOR_STARTUP_METHOD_VALUE = "motor_startup_method_value";






    private int stByte = 0; // 单相、三相
    private int mainVersionByte = 0; // 主版本号
    private int subVersionByte = 0;// 子版本号
    private int realByte = 0;// 是否正品
    private int irefByte = 0;// 额定电流
    private int fjcByte = 0;// 第一次校准电流
    private int sjcByte = 0;// 第二次校准电流
    private int tanlowByte = 0;// Tan 低字节
    private int tanhighByte = 0;// Tan 高字节
    private int vclByte = 0;// 电压参考漏电值
    private int shutlByte = 0;// 关闭漏电
    private int gqdelayByte = 0;// 关枪延时 秒
    private int agjdelayByte = 0;// 自动关机延时 分
    private int gqmodeByte = 0;// 关机模式
    private int bdateyByte = 0;// 生产日期年
    private int bdatemByte = 0;// 生产日期月
    private int bdatedByte = 0;// 生产日期日
    private int mrhhByte = 0;// 电机运行高字节
    private int mrhlByte = 0;// 电机运行低字节
    private int mrmByte = 0;// 电机运行 分
    private int fsByte = 0; // 频率选择
    private int snhByte = 0;// 开关次数高字节
    private int snlByte = 0;// 开关次数低字节
    private int oleByte = 0;// 过载使能
    private int jdq1Byte = 0;// 继电器参数 1
    private int jdq2Byte = 0;//
    private int jdq3Byte = 0;//
    private int jdq4Byte = 0;//
    private int lrtByte = 0;// 最长运转时间 分
    private int snsByte = 0;// 开关机次数标志
    private int empvlByte = 0;// 空载中性参考低8
    private int empvhByte = 0;//
    private int fmpvlByte = 0;// 满载中性参考低8
    private int fmpvhByte = 0;//
    private int ogdelayByte = 0;//
    private int psignByte = 0;//
    private int gmhByte = 0;//
    private int gmlByte = 0;//
    private int drtmsignByte = 0; //
    private int drtsByte = 0; //
    private int waterhhByte = 0;
    private int waterlhByte = 0;
    private int watermByte = 0;
    private int waterHsByte = 0;
    private int watersByte = 0;
    private int degreeHByte = 0;
    private int degreeLByte = 0;
    private int jorHByte = 0;
    private int jorLByte = 0;

    private static String DEVICE_STATUS = "";

    private boolean isSingle = true;

    // 解析一
    public void handleMotoMessage(byte[] msg) {

        int max_size = msg.length;

        stByte = msg[2] & 0xff;
        mainVersionByte = msg[3] & 0xff;
        subVersionByte = msg[4] & 0xff;
        realByte = msg[5] & 0xff;
        irefByte = msg[6] & 0xff;
        fjcByte = msg[7] & 0xff;
        sjcByte = msg[8] & 0xff;
        tanlowByte = msg[9] & 0xff;
        tanhighByte = msg[10] & 0xff;
        vclByte = msg[11] & 0xff;
        shutlByte = msg[12] & 0xff;
        gqdelayByte = msg[13] & 0xff;
        agjdelayByte = msg[14] & 0xff;
        gqmodeByte = msg[15] & 0xff;
        bdateyByte = msg[16] & 0xff;
        bdatemByte = msg[17] & 0xff;
        bdatedByte = msg[18] & 0xff;

        mrhhByte = msg[19] & 0xff;
        mrhlByte = msg[20] & 0xff;
        mrmByte = msg[21] & 0xff;

        fsByte = msg[22] & 0xff;

        snhByte = msg[23] & 0xff;
        snlByte = msg[24] & 0xff;
        oleByte = msg[25] & 0xff;

        jdq1Byte = msg[26] & 0xff;
        jdq2Byte = msg[27] & 0xff;
        jdq3Byte = msg[28] & 0xff;
        jdq4Byte = msg[29] & 0xff;

        lrtByte = msg[30] & 0xff;

        snsByte = msg[31] & 0xff;
        empvlByte = msg[32] & 0xff;
        empvhByte = msg[33] & 0xff;
        fmpvlByte = msg[34] & 0xff;
        fmpvhByte = msg[35] & 0xff;

        ogdelayByte = msg[36] & 0xff;
        psignByte = msg[37] & 0xff;

        gmlByte = msg[38] & 0xff;
        gmhByte = msg[39] & 0xff;

        drtmsignByte = msg[40] & 0xff;
        drtsByte = msg[41] & 0xff;

        waterhhByte = msg[42] & 0xff;
        waterlhByte = msg[43] & 0xff;
        watermByte = msg[44] & 0xff;
        waterHsByte = msg[45] & 0xff;
        watersByte = msg[46] & 0xff;

        degreeHByte = msg[48] & 0xff;
        degreeLByte = msg[47] & 0xff;
        jorHByte = msg[50] & 0xff;
        jorLByte = msg[49] & 0xff;

    }

    public boolean isModorDeviceInfoMessage(byte[] msg) {
        if (msg[2] == (byte) 0x8f || msg[2] == 0x0f) {
            return true;
        }
        return false;
    }

    public String getSingleStatus() {
        if (stByte == 0x0f) {
            return "单相";
        } else {
            return "三相";
        }

    }

    public String getMotorVersion() {
        return "V" + String.format("%02x-%02x", mainVersionByte, subVersionByte);
    }

    public String isMotorReal() {
        return (realByte == 1) ? "正品" : "非正品";
    }

    public String getIrefCurrent() {
        String string = String.format("%.1f 安", irefByte / 10.0);

        Log.i("bear", string);

        return string;

    }

    public float IrefCurrent() {
        return (float) (irefByte / 10.0);
    }

    public String getfirstAjustCurrent() {
        return String.format("%.1f", (fjcByte / 10.0));

    }

    public String getsecondAjustCurrent() {
        return String.format("%.1f", (sjcByte / 10.0));
    }

    public int getTanValue() {
        return tanlowByte + tanhighByte * 256;
    }

    public String getLrefVolt() {
        return String.format("%.3f", vclByte * 3.3 / 1024);
    }

    public String isEnableDetectLeak() {

        if (shutlByte != 1) {

            return "启用";
        } else {

            return "关闭";
        }
    }

    public String getshutGunDelayTime() {
        return String.format("%d", gqdelayByte) + " 秒";
    }

    public int getsgDelay() {
        return gqdelayByte;
    }

    public String getautoShutdownTime() {

        if (agjdelayByte == 0) {
            return "不启用";

        } else {
            return String.format("%d", agjdelayByte) + " 分";
        }
    }

    public String getsjdelayTime() {

        if (agjdelayByte == 0) {
            return "不启用";

        } else {
            return String.format("%d", agjdelayByte);
        }
    }

    public String getstartUpMethod() {
        String typeString = "关闭";

        switch (gqmodeByte) {
            case 0:
                typeString = "常闭";

                break;

            case 1:
                typeString = "常开";
                break;

            case 2:
                typeString = "无线";

                break;

            default:
                typeString = "手动";
                break;

        }


        return typeString;
    }

    public void setDeviceStatus(String status) {
        DEVICE_STATUS = status;
    }

    public static String getDeviceStatus() {
        return DEVICE_STATUS;
    }

    public String getproductDateString() {

        return String.format("%d-%02d-%02d", bdateyByte + 2000, bdatemByte, bdatedByte);

    }

    // 返回时间为秒,返回电机运行时间
    public String getmotorRunedTime() {

        int hour = mrhhByte * 256 + mrhlByte;
        int min = mrmByte;

        return String.format("%d小时%d分", hour, min);
    }

    public String getworkingFreq() {
        return (fsByte == 1) ? "60 Hz" : "50Hz";
    }

    public String getstartTimes() {

        // byte[31] << 16 + byte[23] << 8 + byte[24]
        // snsByte << 16 此写法结果为零造成错误
        return String.format("%d 次", snsByte * 256 * 256 + snhByte * 256 + snlByte);
    }

    public String isEnableOverloadShield() {

        if (oleByte == 0) {
            return "启用";
        } else {
            return "关闭";
        }

    }

    public int getMaxContinueRuningTime() {
        return lrtByte;
    }

    public String getStartGunDelayTime() {
        return String.format("%.1f 秒", ogdelayByte / 10.0);
    }

    public float getogDelay() {
        return (float) (ogdelayByte / 10.0);
    }

    public String getMotorProductorCode() {

        String productor = "";

        switch (psignByte) {
            case 0x00:
                productor = "上海西阔电子";
                break;
            case 0x01:
                productor = "上海熊猫机械";
                break;

            default:
                productor = "未知厂商";

        }

        return productor;

    }

    public String getMotorSerialId() {
        return String.format("%02X%02X%02X%02X%04X", psignByte, bdateyByte, bdatemByte, bdatedByte, gmhByte * 256 + gmlByte);
    }

    public String getMotorUsedEnergy() {
        float total = degreeHByte * 256 + degreeLByte;
        total += (float) (jorHByte * 256 + jorLByte) / 36000;
        String e = String.format("%.2f", total);

        return e;
    }

    // 分钟
    public float getMotorUsedWaterGunTime() {

        return (waterHsByte * 1600 + (waterhhByte * 256 + waterlhByte)) * 60 + watermByte + (watersByte * (float) 1.0) / 60;
    }

    public int getFullLoadPara() {
        return fmpvhByte * 256 + fmpvlByte;
    }

    public int getEmptyLoadPara() {
        return empvhByte * 256 + empvlByte;
    }

}
